use std::string::ToString;
use salvo::{handler, Depot, FlowCtrl, Request, Response, Router};
use salvo::logging::Logger;
use crate::domain::dto::result_to_custom;
use crate::domain::entity::user_entity::UserEntity;
use crate::routers::biz_router::{biz_add, biz_delete, biz_pages, biz_update};
use crate::routers::group_router::{group_add, group_delete, group_update};
use crate::routers::project_router::{project_create, project_delete, project_pages, project_update};
use crate::routers::login_router::{captcha, login};
use crate::routers::user_router::{page_list, user_add, user_delete};
use crate::service::redis_service;

mod user_router;
mod login_router;
mod group_router;
mod biz_router;
mod project_router;
mod module_router;

/// init_routes 初始化路由
pub(super) fn init_routes() -> Router {
    let router = Router::new().hoop(Logger::new());
    router.push(Router::with_path("xxl-api-admin")
        .push(Router::new()
            .push(Router::with_path("login").post(login))
            .push(Router::with_path("captcha").get(captcha))
        )
        .push(Router::new()
            // jwt认证
            // .hoop(auth_token)
            .push(Router::with_path("user")// 添加用户
                .push(Router::with_path("add").post(user_add))
                // 用户分页查询
                .push(Router::with_path("pages").get(page_list))
                // 删除用户
                .push(Router::with_path("delete").delete(user_delete))
            )
            // 项目模块
            .push(Router::with_path("biz")
                // 分页
                .push(Router::with_path("pages").get(biz_pages))
                // 创建项目
                .push(Router::with_path("add").get(biz_add))
                // 更新项目
                .push(Router::with_path("update").get(biz_update))
                // 删除项目
                .push(Router::with_path("delete").get(biz_delete))
            )
            // 项目模块
            .push(Router::with_path("project")
                // 分页
                .push(Router::with_path("pages").post(project_pages))
                // 创建项目
                .push(Router::with_path("create").post(project_create))
                // 更新项目
                .push(Router::with_path("update").post(project_update))
                // 删除项目
                .push(Router::with_path("delete").get(project_delete))
            )
            // 分组模块
            .push(Router::with_path("group")
                // 创建分组
                .push(Router::with_path("add").get(group_add))
                // 更新分组
                .push(Router::with_path("update").get(group_update))
                // 删除分组
                .push(Router::with_path("delete").get(group_delete))
            )
        )
    )
}

/// auth_token token 验证
#[handler]
async fn auth_token(req: &mut Request, res: &mut Response, ctrl: &mut FlowCtrl, depot: &mut Depot) {
    if let Some(token) = req.headers().get("Authorization") {
        let s= token.to_str().unwrap().to_string().replace("Bearer ","");
        match redis_service::get::<UserEntity, String>(s) {
            Err(_) => {
                ctrl.skip_rest();
                res.render(result_to_custom::<()>(401, "认证失败".to_string()))
            },
            Ok(user) => {
                depot.insert("user",user);
            }
        }
    } else {
        ctrl.skip_rest();
        res.render(result_to_custom::<()>(401, "认证失败".to_string()))
    }
}